home *** CD-ROM | disk | FTP | other *** search
- /* A minimal Wavefront to Rayshade converter */
- /* specifically, the minimum necessary to import Viewpoint files */
- /* written because I got sick of fighting with obj2nff.awk & nff2shade.awk */
-
- /* cc obj2ray.c -o obj2ray
- /* Usage: obj2ray <input_file.obj> > <output_file.ray>
-
- /* Jeff Katcher, katcher@netcom.com */
- /* 9/2/93 */
-
- #include <stdio.h>
- #include <string.h>
-
- #define LINELEN 255
- #define NEWLINE '\n'
-
- typedef struct {
- float x,y,z;
- } vertex;
-
- vertex *vertices;
- int vertice_count;
-
- vertex *vnormals;
- int normal_count;
-
- void strip_newline(line)
- char *line;
- {
- char *s;
-
- if (s=strrchr(line,NEWLINE))
- *s=NULL;
- }
-
- int add_vertex(line)
- char *line;
- {
- if (!vertice_count)
- vertices=(vertex *)malloc(sizeof(vertex));
- else
- vertices=(vertex *)realloc(vertices,sizeof(vertex)*(vertice_count+1));
-
- sscanf(line,"v %f %f %f",&(vertices[vertice_count].x),
- &(vertices[vertice_count].y),&(vertices[vertice_count].z));
-
- return(++vertice_count);
- }
-
- int add_vertex_normal(line)
- char *line;
- {
- if (!normal_count)
- vnormals=(vertex *)malloc(sizeof(vertex));
- else
- vnormals=(vertex *)realloc(vnormals,sizeof(vertex)*(normal_count+1));
-
- sscanf(line,"vn %f %f %f",&(vnormals[normal_count].x),
- &(vnormals[normal_count].y),&(vnormals[normal_count].z));
-
- return(++normal_count);
- }
-
- int count_sides(line)
- char *line;
- {
- char *s;
- int count=0;
-
- s=strdup(line);
-
- strtok(s," "); /* skip 'f' type identifier */
- while (strtok(NULL," "))
- count++;
-
- free(s);
-
- return(count);
- }
-
- void parse_facet(line)
- char *line;
- {
- char *token;
- char *split;
- int vertice;
- int normal;
- int sides;
-
- sides=count_sides(line);
-
- if (sides==3)
- printf("triangle\n");
- else
- printf("poly\n");
-
- strtok(line," "); /* skip 'f' type identifier */
- while (token=strtok(NULL," "))
- {
- vertice=atoi(token)-1;
- printf("%f %f %f",vertices[vertice].x,
- vertices[vertice].y,vertices[vertice].z);
-
- if (sides==3 && (split=strstr(token,"//")))
- {
- normal=atoi(split+2)-1;
- printf(" %f %f %f",vnormals[normal].x,vnormals[normal].y,
- vnormals[normal].z);
- }
-
- printf("\n");
- }
- }
-
- void convert_comment(line)
- char *line;
- {
- char *s;
-
- printf("/* %s */\n",(s=strchr(line," "))?s+1:line+1);
- }
-
- main(argc,argv)
- int argc;
- char **argv;
- {
- FILE *fp;
- char line[LINELEN+1];
- char *s;
-
- if (argc!=2)
- fprintf(stderr,"obj2ray: obj2ray <input.obj>\n");
- else
- if (!(fp=fopen(argv[1],"r")))
- fprintf(stderr,"obj2ray: could not open '%s' for input\n",argv[1]);
- else
- {
- while (fgets(line,LINELEN,fp))
- {
- strip_newline(line);
-
- switch (*line)
- {
- case '#': /* comment */
- case '$': /* seems to be comment */
- convert_comment(line);
- break;
-
- case 'v': /* vertex */
- switch (*(line+1))
- {
- case ' ': /* regular vertex 'v' */
- add_vertex(line);
- break;
-
- case 'n': /* vertex normal 'vn' */
- add_vertex_normal(line);
- break;
-
- default: /* unsupported vertex type */
- fprintf(stderr,"Unsupported vertex type: '%s'\n",line);
- }
- break;
-
- case 'g': /* group */
- convert_comment(line);
- break;
-
- case 's': /* smoothing group # */
- /* convert_comment(line); */
- break;
-
- case 'f': /* facet */
- parse_facet(line);
- break;
-
- case NULL: /* Empty line */
- printf("\n");
- break;
-
- default:
- fprintf(stderr,"Error: cannot handle: '%s'\n",line);
- }
- }
-
- fclose(fp);
-
- printf("/* obj2ray: created %d vertices */\n",vertice_count);
- printf("/* obj2ray: created %d vertex normals */\n",normal_count);
- }
- }
-